package com.example.qu_tao.Util;

import android.util.Log;

import com.example.qu_tao.entity.CaiGouInfo;

import java.util.ArrayList;
import java.util.List;

public class CaiGouDataManager {
    private static volatile CaiGouDataManager instance;
    private final List<CaiGouInfo> caiGouInfoList;

    private CaiGouDataManager() {
        caiGouInfoList = new ArrayList<>();
        // 初始化一些测试数据
        initTestData();
    }

    public static CaiGouDataManager getInstance() {
        if (instance == null) {
            synchronized (CaiGouDataManager.class) {
                if (instance == null) {
                    instance = new CaiGouDataManager();
                }
            }
        }
        return instance;
    }

    // 添加采购信息
    public void addCaiGouInfo(CaiGouInfo caiGouInfo) {
        synchronized (this) {
            caiGouInfoList.add(0, caiGouInfo);
        }
    }

    // 获取所有采购信息
    public List<CaiGouInfo> getAllCaiGouInfo() {
        return new ArrayList<>(caiGouInfoList);
    }

    // 获取指定ID的采购信息
    public CaiGouInfo getCaiGouInfoById(String id) {
        for (CaiGouInfo info : caiGouInfoList) {
            if (info.getId().equals(id)) {
                return info;
            }
        }
        return null;
    }

    // 初始化测试数据
    private void initTestData() {
        CaiGouInfo info1 = new CaiGouInfo();
        info1.setId("1");
        info1.setType("农产品");
        info1.setVariety("蔬菜");
        info1.setQuantity("1000");
        info1.setUnit("斤");
        info1.setSource("全国");
        info1.setCycle("周采购");
        info1.setRegion("北京");
        info1.setDescription("采购新鲜蔬菜，每周一次");
        info1.setPublishTime("2025-06-22 10:30:00");
        caiGouInfoList.add(info1);

        CaiGouInfo info2 = new CaiGouInfo();
        info2.setId("2");
        info2.setType("食品");
        info2.setVariety("大米");
        info2.setQuantity("500");
        info2.setUnit("公斤");
        info2.setSource("华东");
        info2.setCycle("月采购");
        info2.setRegion("上海");
        info2.setDescription("采购优质大米，每月一次");
        info2.setPublishTime("2025-06-21 15:45:00");
        caiGouInfoList.add(info2);

        Log.d("CaiGouDataManager", "Test data initialized, size: " + caiGouInfoList.size());
    }
}